#include "tcp.h"

int main(int argc, char*argv[]){

	int fd;
	int ret;
	char buf[BUFSIZ] = {"====test===="};
	Addr_in addr;

	/*检查参数*/
	if(argc < 3){
		fprintf(stderr, 
				"%s <addr><port>\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	/*创建套接字*/
	if( (fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		ErrExit("socket");
	bzero(&addr,sizeof(addr) );
	
	/*设置地址和ip端口号*/
	addr.sin_family = AF_INET;
	addr.sin_port = htons( atoi(argv[2]) );
	if( inet_aton(argv[1], &addr.sin_addr)  == 0 ){
		fprintf(stderr, "Invalid address\n");
		exit(EXIT_FAILURE);
	}

	/*发起连接请求*/
	if( connect(fd, (Addr *)&addr, sizeof(addr) ) )
		ErrExit("connect");

	/*发送数据*/
	while(1){
		do {
			ret = send(fd, buf, BUFSIZ, 0);
		}while(ret < 0 && errno == EINTR);
		if(ret < 0)
			ErrExit("recv");
		else if(!ret)
			break;
		printf("send data:%s", buf);
		fflush(stdout);
		getchar();
	}

	close(fd);	
	return 0;
}
